home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / pmpsrc11.zip / TRACE.C < prev    next >
Text File  |  1991-07-30  |  4KB  |  171 lines

  1. /*
  2.     TRACE -- Packet tracing routines (for debugging)
  3.  
  4.   Poor Man's Packet (PMP)
  5.   Copyright (c) 1991 by Andrew C. Payne    All Rights Reserved.
  6.  
  7.   Permission to use, copy, modify, and distribute this software and its
  8.   documentation without fee for NON-COMMERCIAL AMATEUR RADIO USE ONLY is hereby
  9.   granted, provided that the above copyright notice appear in all copies.
  10.   The author makes no representations about the suitability of this software
  11.   for any purpose.  It is provided "as is" without express or implied warranty.
  12.  
  13.     August, 1989
  14.  
  15.     Andrew C. Payne
  16. */
  17.  
  18. /* ----- Includes ----- */
  19. #include <stdio.h>
  20. #include <alloc.h>
  21. #include <mem.h>
  22. #include <dos.h>
  23. #include <bios.h>
  24. #include <time.h>
  25. #include "pmp.h"
  26.  
  27. #ifdef TRACE
  28.  
  29. #define    LOGSIZE    1000            /* number of packets in log */
  30.  
  31. extern char    *DumpLevel2(struct ax25_packet *p);
  32. extern struct ax25_packet *AX25L1toL2(struct ax25_level1 *p);
  33.  
  34. /* ------ Local Structures ----- */
  35. struct packet_log {
  36.     long        time;        /* time sent */
  37.     int        dir;        /* direction */
  38.     struct packet_log    *next;    /* next packet in linked list */
  39.     struct ax25_level1 p;        /* the level 1 packet */
  40. };
  41.  
  42. static struct packet_log **log;
  43.     int    nlogs;            /* number of items in log */
  44.  
  45. static FILE    *outfile;        /* output file */
  46.  
  47. /* LogInit()
  48.     Initialize the logging system.
  49. */
  50. void LogInit()
  51. {
  52.     log = malloc(sizeof(struct packet_log *) * LOGSIZE);
  53.     nlogs = 0;
  54. }
  55.  
  56. /* LogPacket(p,dir)
  57.     Given a pointer to a level 1 packet, allocates a record for the packet
  58.     and stores the packet, direction (incoming or outgoing), and the time.
  59. */
  60. void LogPacket(struct ax25_level1 *p, int dir)
  61. {
  62.     struct packet_log    *pl;
  63.  
  64. /* log full? */
  65.     if(nlogs >= LOGSIZE)
  66.         return;
  67.  
  68. /* allocate a structure */
  69.     pl = malloc(sizeof(struct packet_log) + sizeof(struct ax25_level1) + p->len);
  70.     if(pl == NULL) {
  71.         uprintf(BrightAttr,"--- Out of memory in LogPacket\n");
  72.         return;
  73.     }
  74.  
  75. /* set up the log packet */
  76.     pl->time = BiosTime();
  77.     pl->dir = dir;
  78.     memcpy(&pl->p,p,sizeof(struct ax25_level1) + p->len);
  79.  
  80. /* insert into log */
  81.     log[nlogs++] = pl;
  82. }
  83.  
  84. /* DumpStatus()
  85.     Dumps the current PMP status variables to the log file.
  86. */
  87. static void DumpStatus(void)
  88. {
  89.     long    t;
  90.     struct tm    *tm;
  91.     int    hours,minutes,secs;
  92.  
  93.     time(&t);
  94.     tm = localtime(&t);
  95.     fprintf(outfile,"\n----------\n");
  96.     fprintf(outfile,"PMP Status at %02d/%02d/%02d %02d:%02d:%02d  ",
  97.         tm->tm_mon+1, tm->tm_mday, tm->tm_year, tm->tm_hour,
  98.         tm->tm_min, tm->tm_sec);
  99.     t -= StartTime;
  100.     secs = t % 60;
  101.     t /= 60;
  102.     minutes = t % 60;
  103.     t /= 60;
  104.     hours = t % 24;
  105.     t /= 24;
  106.     fprintf(outfile,"    Uptime %d %02d:%02d:%02d\n",
  107.         (int)t, hours, minutes, secs);
  108.     fprintf(outfile,"      %8ld good frames received\n",RXCount);
  109.     fprintf(outfile,"      %8ld framing errors\n",RXFrameErr);
  110.     fprintf(outfile,"      %8ld checksum errors\n",RXCRCErr);
  111.     fprintf(outfile,"      %8ld receive queue overflows\n",RXQOverflow);
  112.     fprintf(outfile,"      %8ld receive buffer overflows\n",RXBOverflow);
  113.     fprintf(outfile,"      %8ld REJ frames received\n",RXREJ);
  114.     fprintf(outfile,"      %8ld FRMR frames received\n",RXFRMR);
  115.     fprintf(outfile,"      %8ld frames transmitted\n",TXCount);
  116.     fprintf(outfile,"      %8ld frames in trace log\n",(long)nlogs);
  117.     fprintf(outfile,"  %12ld bytes free\n\n",coreleft());
  118. }
  119.  
  120. /* DumpEntry(pl)
  121.     Dumps a log entry to the output file.
  122. */
  123. static void DumpEntry(struct packet_log *pl)
  124. {
  125.     struct ax25_packet    *p;
  126.     static long    lasttime = 0;
  127.     long    t;
  128.  
  129. #ifdef HEX
  130.     int    i;
  131.     for(i=0; i<pl->p.len; i++)
  132.         fprintf(outfile, "%X ",pl->p.data[i]);
  133.     fprintf(outfile,"\n\n");
  134. #else
  135.     putchar('.');
  136.     p = AX25L1toL2(&pl->p);
  137.     putchar('-');
  138.     t = pl->time - lasttime;
  139.     lasttime = pl->time;
  140.     fprintf(outfile,"[+%ld]  %s\n",t,DumpLevel2(p));
  141. #endif
  142.     free(pl);
  143. }
  144.  
  145. /* DumpLog()
  146.     Dumps all of the log entries to logfile.
  147. */
  148. void DumpLog(void)
  149. {
  150.     char    fname[80];
  151.     int    i;
  152.  
  153.     if(nlogs) {
  154.         printf("\n\nFilename for trace log (RETURN for no logfile) --> ");
  155.         gets(fname);
  156.         if(*fname) {
  157.             if((outfile = fopen(fname,"w")) == NULL) {
  158.                 perror("Can't open output file");
  159.                 return;
  160.             }
  161.             fprintf(outfile,"Version:  PMP %s compiled %s\n",VERSION,__DATE__);
  162.             for(i = 0; i<nlogs; i++)
  163.                 DumpEntry(log[i]);
  164.             DumpStatus();
  165.             printf("\n%d packets written to logfile.\n",nlogs);
  166.             fclose(outfile);
  167.         }
  168.     }
  169. }
  170. #endif    /* TRACE */
  171.